//使用asyc和await完成需求
 function sleep(time) {
    return new Promise((resolve,reject)=> {
        // console.log(new Date,time)
        setTimeout(resolve,time)
    })
}

// async function aprint() {
//     for(var i = 0;i<5;i++) {
//         if (i > 0) {
//             await sleep(1000);
//         }
//         console.log(new Date, i);
//     }

//     await sleep(1000)
//     console.log(new Date,i)
   
// }

function Person(name) {
  this.name = name
  console.log('Hi,this is '+this.name)
}

Person.prototype.eat = function(which) {
  console.log(`Eat ${which}`)
  return this
}
Person.prototype.sleep = async function(delay) {

 await setTimeout(()=>{
  console.log(`weak up ${delay}秒`)
 },delay*1000)
 return this 
  
}

new Person('liang').sleep(2).eat('super')


class PersonGenerator {
  taskQueue = [];
  constructor(name) {
    this.taskQueue.push(() => this.sayHi(name));
    this.runTaskQueue();
  }
  nextTask = () => {
    if (this.taskQueue.length > 0) {
      const task = this.taskQueue.shift();
      if (typeof task === "function") {
        task();
        this.nextTask();
      }
      if (typeof task === "number") {
        console.log(`Sleep ${task} seconds \n`);
        setTimeout(() => this.nextTask(), task * 1000);
      }
    }
  };

  runTaskQueue = () => {
    setTimeout(() => this.nextTask());
  };

  sayHi(name) {
    console.log(`Hi! This is ${name}! \n`);
    return this;
  }

  sleep(seconds) {
    this.taskQueue.push(seconds);
    return this;
  }

  sleepFirst(seconds) {
    this.taskQueue.splice(-1, 0, seconds);
    return this;
  }

  eat(food) {
    this.taskQueue.push(() => console.log(`Eat ${food}~ \n`));
    return this;
  }
}

const Person = name => new PersonGenerator(name);

Person("helloWorld").sleepFirst(3).sleep(3).eat("little_cute");